package com.zcz.authentication.ctl;

import com.zcz.authentication.bean.User;
import com.zcz.authentication.service.ShiroService;
import com.zcz.authentication.util.FebsException;
import com.zcz.authentication.util.JwtUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


//@RestController
@Controller
@RequestMapping("holle/")
public class UserCtl {
	@Value("${rsa.privatekey}")
	private String privatekey;
	@Autowired
	private ShiroService shiroService;
	@Autowired
	private ShiroFilterFactoryBean shiroFilterFactoryBean;
	@ResponseBody
	@RequestMapping("holle")
	public String holle() throws FebsException {
		try {
			System.err.println("++++++++");
			return "ok";
		} catch (Exception e) {
			String message = "新增部门失败";
            //log.error(message, e);
            throw new FebsException(message);
		}
	}
	@RequestMapping("thymeleaf")
	public String thymeleaf(Model model,String tokenid) {
		System.err.println("++++++++");
		model.addAttribute("test",tokenid);
		return "thymeleaf";
	}
	//添加
	@RequestMapping("add")
	public String add() {
		return "user/add";
	}
	//更新权限
	@RequestMapping("gengxin")
	public String gengxin() {
		try {
			shiroService.updatePermission(shiroFilterFactoryBean);
		} catch (FebsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "user/gengxin";
	}
	//更新
	@RequestMapping("update")
	public String update() {
		System.err.println("update");
		return "user/update";
	}
	//删除
	@RequestMapping("del")
	public String del() {
		System.err.println("del");
		return "user/del";
	}
	//详情
	@RequestMapping("details")
	public String details() {
		return "user/details";
	}
	
	@RequestMapping("toLogin")
	public String toLogin() {
		return "/login";
	}
	//退出
	@RequestMapping("logout")
	public String logout() {
		SecurityUtils.getSubject().logout();
		return "/login";
	}
	//跳转未授权页面
	@RequestMapping("noAuth")
	public String noAuth() {
		return "/noAuth";
	}
	/**
	 * 登录
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping("login")
	public String login(String name,String password,Model model) {
		/**
		 * 使用shiro编写认证操作
		 */
		//获取subject
		org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
		//封装用户数据
		UsernamePasswordToken token = new UsernamePasswordToken(name,password);
		//执行登录方法
		try {
			subject.login(token);
			User user = (User) SecurityUtils.getSubject().getPrincipal();
			//创建tokenid
			Map<String,Object> map = new HashMap<>();
			map.put("userid",user.getId());
			map.put("username", user.getName());
			String tokenId = "";
			try {
				tokenId = JwtUtil.createJWT(map,"",1000*60*60*12);//过期时间1天
			} catch (Exception e) {
				throw new RuntimeException("创建Token失败");
			}
			//登录成功 跳转到thymeleaf
			return "redirect:/holle/thymeleaf?tokenid="+tokenId+"";
			
		} catch (UnknownAccountException e) {
			//登录失败  用户名不存在
			model.addAttribute("msg"," 用户名不存在");
			return "login";
		}catch(IncorrectCredentialsException e){  
			model.addAttribute("msg", "密码不正确");  
            return "login";//返回登录页面
		}
	}
}
